时间和日期转换函数 您所在的位置:网站首页 date 时间戳转时间 时间和日期转换函数

时间和日期转换函数

2023-04-14 16:25| 来源: 网络整理| 查看: 265

Hologres兼容PostgreSQL,支持使用标准的PostgreSQL语法进行开发。本文为您介绍Hologres已支持的时间和日期函数列表及使用用例。

Hologres已支持的时间和日期函数列表如下。当前Hologres版本支持的函数是PostgreSQL的一个子集,函数的使用方法请参见时间和日期函数和时间格式化函数。

时间日期转换函数to_char(timestamp/timestamptz, text)返回类型:TEXT描述:将时间戳转换为字符串,默认支持时间范围为1925~2282年。YYYY对应年,MM对应月,DD对应日,HH对应时,MI对应分,SS对应秒。可以使用to_char函数进行24小时制和12小时制的转换,HH24对应24小时制,HH12对应12小时制,默认为12小时制。从Hologres V1.1.31版本开始,在SQL前执行set hg_experimental_functions_use_pg_implementation = 'to_char';或者set hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp';可支持所有时间。 说明 使用该GUC参数后,查询性能约有50%的损失,升级至Hologres V1.1.42及以上版本后,约有20%的损失。使用示例:示例场景示例请求返回结果将时间戳转换为24小时制select to_char(current_timestamp, 'HH24:MI:SS'); select to_char(current_timestamp, 'YYYY-MM-DD');18:26:33 20221208将时间戳转换为12小时制select to_char(current_timestamp, 'HH12:MI:SS AM'); select to_char(time '00:30:00', 'HH12:MI:SS AM');18:26:33 PM 12:30:00 AM带字段的时间戳转换CREATE TABLE time_test( a text, b TIMESTAMPTZ ); INSERT INTO time_test VALUES ('2001-09-28 03:00:00','2004-10-19 10:23:54+08'); select to_char(b, 'HH24:MI:SS') FROM time_test; --将text的字段转换为日期 select to_char(to_timestamp(a, 'YYYY-MM-DD'),'YYYY-MM-DD')FROM time_test;10:23:54 2001-09-28跨时区的时间戳转换CREATE TABLE timestamptz_test( a TIMESTAMPTZ); INSERT INTO timestamptz_test VALUES ('2023-03-21 10:23:54+02'); --不指定时区,则默认将a转为系统时区(东八区)后转换为字符串 select to_char(a, 'YYYY-MM-DD HH24:MI:SS') FROM timestamptz_test; --指定时区为美东时区后转换为字符串 select to_char(a at time zone 'US/Eastern', 'YYYY-MM-DD HH24:MI:SS') FROM timestamptz_test;2023-03-21 16:23:54 2023-03-21 04:23:54to_char(int, text)返回类型:TEXT描述:

将整数转换为字符串。

使用示例请求命令返回结果select to_char(125, '999');125to_char(double precision, text)返回类型:TEXT描述:

将实数或双精度数转换为字符串。

使用示例请求命令返回结果select to_char(125.8::real, '999D9');125.8to_date(text, text)返回类型:DATE描述:

将字符串转换为日期,默认支持时间范围为1925~2282年。

从Hologres V1.1.31版本开始,在SQL前执行set hg_experimental_functions_use_pg_implementation = 'to_date';或者set hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp';可支持所有时间。说明 使用该GUC参数后,查询性能约有50%的损失,升级至Hologres V1.1.42及以上版本后,约有20%的损失。使用示例:示例场景示例请求返回结果字符串转换为日期select to_date('05 Dec 2000', 'DD Mon YYYY'); select to_date('2001 03 24', 'YYYY-MM-DD');2000-12-05 2001-03-24text类型的表字段转换为日期create table time_test(a text); insert into time_test valuse ('2001-09-28 03:00:00'); select to_date(a, 'YYYY-MM-DD') from time_test;2001-09-28to_timestamp(text, text)返回类型:TIMESTAMPTZ描述:将字符串转换为时间戳,默认支持时间范围为1925~2282年。执行结果包含+08。从Hologres V1.1.31版本开始,在SQL前执行set hg_experimental_functions_use_pg_implementation = 'to_timestamp';或者set hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp';可支持所有时间。说明 使用该GUC参数后,查询性能约有50%的损失,升级至Hologres V1.1.42及以上版本后,约有20%的损失。使用示例:示例场景示例请求返回结果字符串转换为时间select to_timestamp('05 Dec 2000', 'DD Mon YYYY');2000-12-05 00:00:00+08将字符串类型转换为时间戳create table time_test(a text); insert into time_test values ('2001-09-28 03:00:00'); select to_timestamp(a, 'YYYY-MM-DD') from time_test;2001-01-09 00:00:00+08to_timestamp(double precision)返回类型:TIMESTAMPTZ描述:将时间戳转换为日期。说明 从1970-01-01 00:00:00+00的秒数开始转换。使用示例:示例场景示例请求返回结果时间戳为秒的转换select to_timestamp(163280296);1975-03-06 03:38:16+08时间戳为毫秒的转换select to_timestamp(1632802961000/1000);2021-09-28 12:22:41+08时间日期加减乘除函数日期/时间相加:+返回类型用例结果DATE从指定日期开始向前加7天,返回日期类型select date '2001-09-28' + integer '7';2001-10-05当前日期向前加3天,返回日期类型select current_date+ integer '3 ';2022-12-10当前时间向前加1天,返回日期类型select to_char(current_date+ interval '1 day','yyyy-mm-dd');2022-12-09TIMESTAMP从指定日期开始向前加3个小时(0点开始计算),返回timestamp类型select date '2001-09-28' + time '03:00';2001-09-28 03:00:00从指定日期开始向前加1个小时(0点开始计算),返回timestamp类型select date '2001-09-28' + interval '1 hour';2001-09-28 01:00:00TIMESTAMPTZ当前时间向前加1天,返回timestamptz类型select now()+interval '1 day';2022-12-08 20:09:19.388465+08当前时间向前加1个月,返回timestamptz类型 select now()+interval '1 month';2023-01-08 20:21:50.993481+08当前时间向前加2年,返回timestamptz类型 select now()+interval '2 year';2024-12-08 20:22:49.416343+08日期/时间相减:-返回类型用例结果INTEGER指定日期相减,返回integer类型select date '2001-10-01' - date '2001-09-28';3DATE指定日期减7天,返回日期类型select date '2001-10-01' - integer '7';2001-09-24TIMESTAMP指定日期减3个小时 select date '2001-09-28' - time '03:00';2001-09-27 21:00:00指定日期减1个小时 select date '2001-09-28' - interval '1 hour';2001-09-27 23:00:00当前时间减2天select now()-interval '2 day';2022-12-06 20:27:21.094258+08日期/时间相乘:*返回类型用例结果INTERVAL时间相乘select 21 * interval '3 day';0 years 0 mons 63 days 0 hours 0 mins 0.0 secs日期/时间相除:/返回类型用例结果INTERVAL时间相除select interval '1 hour' / double precision '1.5';0 years 0 mons 0 days 0 hours 40 mins 0.0 secs当前日期/时间函数名返回类型描述用例结果current_dateDATE获取当前日期。select current_date;2020-05-03current_timestampTIMESTAMPTZ获取当前事务的开始时刻。说明 在事务的整个运行周期内不改变。select current_timestamp;2020-05-03 06:33:36.113682+08clock_timestamp()TIMESTAMPTZ获取当前时刻。说明 在同一条命令中也会发生变化。select clock_timestamp();2020-05-03 06:32:28.814918+08localtimestampTIMESTAMP获取不包含时区的当前时间。select localtimestamp;2020-08-21 12:02:21.178031now()TIMESTAMPTZ获取当前事务的开始时刻,等效于transaction_timestamp()。说明 在事务的整个运行周期内不改变。select now();2020-05-03 06:38:48.492168+08statement_timestamp()TIMESTAMPTZ获取当前语句的开始时刻。说明 在事务的不同命令中返回值不同。select statement_timestamp();2020-05-05 06:39:11.125957+08timeofday()TEXT获取当前时刻。说明 与clock_timestamp()类似,但时间使用格式化文本字符串格式。select timeofday();Tue May 03 06:39:43.195368 2020 CSTtransaction_timestamp()TIMESTAMPTZ获取当前事务的开始时刻,等效于current_timestamp。说明 在事务的整个运行周期内不改变。select transaction_timestamp();2020-05-03 06:40:08.023623+08时间日期截取函数extract(field from timestamp)返回类型:DOUBLE PRECISION描述:从时间戳中获取子字段。说明 输入的field常量值包括century、day、decade、dow(一周中的第几天,周日为0)、isodow(一周中的第几天,周日为7)、doy(一年中的第几天)、epoch、hour、minute、month、quarter、second、week、year等。使用示例示例场景请求示例返回结果获取指定日期的小时数select extract(hour from timestamp '2001-02-16 20:38:40');20获取当前时间的分钟数select extract(minute from now());55带表字段的计算create table time_test(a text); insert into time_test values ('2001-09-28 03:00:00'); --1970年到字段值时间的秒数 select extract(epoch from to_timestamp(a, 'YYYY-MM-DD')) from time_test;978969600date_part(text, timestamp)返回类型:DOUBLE PRECISION描述:从时间戳中获取子字段,等效于extract(field from timestamp)。说明 输入的field常量值包括century、day、decade、dow(一周中的第几天,周日为0)、isodow(一周中的第几天,周日为7)、doy(一年中的第几天)、epoch、hour、minute、month、quarter、second、week、year等。使用示例示例场景请求示例返回结果获取指定日期的小时数select date_part('hour', timestamp '2001-02-16 16:38:40');16从1月1日到指定日期的周数select date_part('week', to_date('20221011', 'YYYY-MM-DD'));41从1月1日到指定日期的月数select date_part('month', to_date('20221011', 'YYYY-MM-DD'));10date_trunc(text,time/timestamp/timestamptz)返回类型:TIMESTAMP/TIMESTAMPTZ描述:截断时间戳到指定精度。说明 输入的text常量值包括century、decade、year、quarter、month、week、day、hour、minute、second等。使用示例示例场景请求示例返回结果截断指定时间戳到小时select date_trunc('hour', time '12:38:40');0 years 0 mons 0 days 12 hours 0 mins 0.0 secs截断指定时间戳到天select date_trunc('day', timestamptz'2001-02-16 20:38:40+08');2001-02-16 00:00:00+08截断指定时间戳到月select date_trunc('month', timestamp '2001-02-16 18:38:40');2001-02-01 00:00:00每月1号 12点select date_trunc('month',now()) +interval '12h';2022-12-01 12:00:00+08每天9点select date_trunc('day',now()) + interval '9h';2022-12-09 09:00:00+08每周的今天select date_trunc('day',now()) + interval '7d';2022-12-16 00:00:00+08每30分钟select date_trunc('minute',now()) + interval '30minute 30second';2022-12-09 11:55:30+08isfinite(date)返回类型:BOOLEAN描述

判断日期是否为有限值,有限值返回true,无限值返回false。

使用示例请求示例返回结果select isfinite(date '2001-02-16');trueisfinite(timestamp)返回类型:BOOLEAN描述

判断时间戳是否为有限值,有限值返回true,无限值返回false。

使用示例请求示例返回结果select isfinite(timestamp '2001-02-16 21:28:30');truemake_date(year int, month int, day int)返回类型:DATE描述

使用年、月、日创建日期。

使用示例:请求示例返回结果select make_date(2013, 7, 15);2013-07-15常用SQL示例N小时内的计算。select now()+interval '2 hour';运行结果:?column? --------------------- 2022-12-29 13:43:58.321104+08日期转换为时间戳。select extract(epoch from current_timestamp);运行结果:date_part --------------------- 1672285506.296279日期字段和数字字段相加。CREATE TABLE date_test1( a DATE, b INT ); INSERT INTO date_test1 VALUES ('2021-09-28','12'); select a + (b || ' month')::interval from date_test1; 运行结果:?column? -------------------- 2022-09-28 00:00:00时间戳转换timestamp。select to_timestamp(to_char(20211027172045,'9999-99-99 99:99:99'),'YYYY-MM-DD HH24:MI:SS');运行结果:to_timestamp ---------------------- 2021-10-27 17:20:45+08时间的截取。select extract(mon from now());运行结果:date_part --------- 12整数相除场景两个整数相除有余数的场景中,Hologres会返回整数舍弃余数。示例:10/3,结果是3,如果需要显示余数,需要显式做类型转换,转成float再计算,如下所示:select 10/3::float;运行结果:?column? --------- 3.3333333333333335


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有